package com.googlecode.jmicroerp.service;

import com.googlecode.jmicroerp.repository.BonRepository;
import java.math.BigDecimal;
import java.util.ArrayList;

import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Alexandru.Codescu
 */
@Service(value = "bonService")
public class BonService {
    @Autowired
    private BonRepository bonRepository;
    
    private Date getStartDate(Date date) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.set(GregorianCalendar.HOUR_OF_DAY, 0);
        calendar.set(GregorianCalendar.MINUTE, 0);
        calendar.set(GregorianCalendar.SECOND, 0);
        calendar.set(GregorianCalendar.MILLISECOND, 0);
        
        return calendar.getTime();
    }
    
    private Date getEndDate(Date date) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.set(GregorianCalendar.HOUR_OF_DAY, 23);
        calendar.set(GregorianCalendar.MINUTE, 59);
        calendar.set(GregorianCalendar.SECOND, 59);
        calendar.set(GregorianCalendar.MILLISECOND, 999);
        
        return calendar.getTime();
    }
    
    private GregorianCalendar getTodayCalendar() {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.set(GregorianCalendar.HOUR_OF_DAY, 0);
        calendar.set(GregorianCalendar.MINUTE, 0);
        calendar.set(GregorianCalendar.SECOND, 0);
        //calendar.set(GregorianCalendar.AM_PM, GregorianCalendar.AM);
        calendar.set(GregorianCalendar.MILLISECOND, 0);
        return calendar;
    }
    
    private Date getTodayDate() {
        return getTodayCalendar().getTime();
    }
    
    private Date addDays(Date date, int days) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);
                 
        return cal.getTime();
    }
    
    private Date get7DayDate() {
        GregorianCalendar calendar = getTodayCalendar();
        calendar.add(GregorianCalendar.DATE, -7);
        
        return calendar.getTime();
    }
    
    private Date getLastMonthDate() {
        GregorianCalendar calendar = getTodayCalendar();
        calendar.set(GregorianCalendar.DATE, 1);
        
        return calendar.getTime();
    }
    
    public List<Object[]> queryBonuriProduseByMagazin(Date dayStart, Date dayEnd, Long idMagazin) {
        Date dayS = getTodayDate();
        Date dayE = addDays(dayS, 1);
        return bonRepository.queryBonuriProduseByMagazin(dayStart, dayEnd, idMagazin);
    }
    
    public List<Object[]> queryBonuriByMagazinToday() {
        Date today = getTodayDate();
        return bonRepository.queryBonuriByMagazin(today);
    }
    
    public List<Object[]> queryBonuriByMagazin7Day() {
        Date sevenDay = get7DayDate();
        return bonRepository.queryBonuriByMagazin(sevenDay);
    }
    
    public List<Object[]> queryBonuriByMagazinMonth() {
        Date lastMonth = getLastMonthDate();
        return bonRepository.queryBonuriByMagazin(lastMonth);
    }
    
    public Object[] queryBonuriToday() {
        Date today = getTodayDate();
        
        List<Object[]> objs = bonRepository.queryBonuri(today);
        if (objs.isEmpty()) {
            return null;
        } else {
            return objs.get(0);
        }
    }
    
    public Object[] queryBonuri7Day() {
        Date sevenDay = get7DayDate();
        List<Object[]> objs = bonRepository.queryBonuri(sevenDay);
        if (objs.isEmpty()) {
            return null;
        } else {
            return objs.get(0);
        }
    }
    
    public Object[] queryBonuriMonth() {
        Date lastMonth = getLastMonthDate();
        List<Object[]> objs = bonRepository.queryBonuri(lastMonth);
        if (objs.isEmpty()) {
            return null;
        } else {
            return objs.get(0);
        }
    }
    
    public BigDecimal queryVanzariAzi() {
        Date today = getTodayDate();
        List<BigDecimal> vanzari = bonRepository.queryVanzariAzi(today);
        if (vanzari.isEmpty()) {
            return BigDecimal.ZERO;
        } else {
            return vanzari.get(0);
        }
    }
    
    public BigDecimal queryVanzariAziByMagazin(Long idMagazin) {
        Date today = getTodayDate();
        List<BigDecimal> vanzari = bonRepository.queryVanzariAziByMagazin(today, idMagazin);
        if (vanzari.isEmpty()) {
            return BigDecimal.ZERO;
        } else {
            return vanzari.get(0);
        }
    }
    
    public BigDecimal queryVanzariByMagazin(Date dayStart, Date dayEnd, Long idMagazin) {
        // Date today = getTodayDate();
        List<BigDecimal> vanzari = bonRepository.queryVanzariByMagazin(dayStart, dayEnd, idMagazin);
        if (vanzari.isEmpty()) {
            return BigDecimal.ZERO;
        } else {
            return vanzari.get(0);
        }
    }
    
    public List<BigDecimal> queryVanzariLast7DaysByMagazin(Long idMagazin) {
        Date date = getTodayDate();
        
        BigDecimal[] values = new BigDecimal[7];
        for (int i = 0; i < 7; i++) {
            BigDecimal value = queryVanzariByMagazin(getStartDate(date), getEndDate(date), idMagazin);
            values[6 - i] = value;
            date = addDays(date, -1);
        }
        
        List<BigDecimal> list = new ArrayList<BigDecimal>();
        Collections.addAll(list, values);
        return list;
    }
    
    public BigDecimal queryCantitateProduseByMagazin(Long idMagazin, Long idProdus) {
        Date today = getTodayDate();
        List<BigDecimal> vanzari = bonRepository.queryCantitateProduseByMagazin(today, idMagazin, idProdus);
        if (vanzari.isEmpty()) {
            return BigDecimal.ZERO;
        } else {
            return vanzari.get(0);
        }
    }
    
    public BigDecimal queryCantitateProduseByMagazinByDay(Long idMagazin, Long idProdus, Date dayStart, Date dayEnd) {
        //Date today = getTodayDate();
        List<BigDecimal> vanzari = bonRepository.queryCantitateProduseByMagazinByDay(idMagazin, idProdus, dayStart, dayEnd);
        if (vanzari.isEmpty()) {
            return BigDecimal.ZERO;
        } else {
            return vanzari.get(0);
        }
    }
    
}
